/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conversor;

import dao.NivelAcademicoDao;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import modelo.NivelAcademico;
import org.primefaces.context.RequestContext;


/**
 *
 * @author bruno
 */
@FacesConverter("nivelAcademicoConversor")
public class NivelAcademicoConversor implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        try {
            return value.isEmpty() ? null : new NivelAcademicoDao().ler(Integer.parseInt(value.split("-")[0]));
        } catch (Exception ex) {
            RequestContext.getCurrentInstance().execute("diag.show()");
            
            return null;
        }
        // System.out.println("CON: " + value );
        // return value.isEmpty() ? null : new PaisDao().ler(Integer.parseInt(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value == null ? "" : (((NivelAcademico) value).getId() + "-" + ((NivelAcademico) value).getDesignacao());
        //return value == null ? "" : Integer.toString( ((Pais)value).getId() ) ;
    }
}
